package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/articlerpc/articlerpc"
)

type Article struct {
	gorm.Model
	Sid     int    `gorm:"type:int(11);not null;comment:类别id"`
	Title   string `gorm:"type:varchar(30);not null;comment:标题"`
	Author  string `gorm:"type:varchar(10);not null;comment:作者名"`
	Content string `gorm:"type:varchar(50);not null;comment:简介"`
}

func (a *Article) ArticleAdd(db *gorm.DB) error {
	return db.Debug().Create(a).Error
}

func (a *Article) FindArticle(db *gorm.DB, title string) error {
	return db.Debug().Where("title=?", title).First(a).Error
}

/*
SELECT

	articles.author,
	articles.content,
	articles.title,
	styles.`name` style_name,
	articles.id article_id

FROM

	articles
	LEFT JOIN styles ON articles.sid = styles.id
*/
func ArticleList(db *gorm.DB) []*articlerpc.ArticleList {
	var list []*articlerpc.ArticleList
	db.Debug().Model(&Article{}).
		Select(
			"articles.author",
			"articles.content",
			"articles.title",
			"styles.`name` style_name",
			"articles.id article_id",
		).Joins("LEFT JOIN styles ON articles.sid = styles.id").
		Find(&list)
	return list
}
